﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;

namespace DuncanDAL
{
    public class BudgetDataService : IDAL<T_BudgetObject>
    {
        public const string SQL_INSERT_T_BUDGET = "insert into T_Budget(BeginDate,"+
            " EndDate,PayCategory,BudgetSum,ReferValue) values("+
            " '{0}','{1}',{2},{3},{4})";

        public const string SQL_DELETE_T_BUDGET = "delete from T_Budget where OID={0}";

        public const string SQL_SELECT_T_BUDGET = "select * from T_Budget";

        public const string SQL_UPDATE_T_BUDGET = "update T_Budget set BeginDate='{0}',"+
            " EndDate='{1}',PayCategory={2},BudgetSum={3},ReferValue={4} where OID={5}";

        public const string SQL_SELECT_SINGLE_T_BUDGET = "select * from T_Budget where OID={0}";

        #region IDAL<T_BudgetObject> 成员

        public void Create(T_BudgetObject obj)
        {
            string sql = string.Format(SQL_INSERT_T_BUDGET, obj.Db_BeginDate, obj.Db_EndDate, obj.Db_PayCategory, obj.Db_BudgetSum, obj.Db_ReferValue);
            DBHelper.Excute(sql);
        }

        public void Delete(T_BudgetObject obj)
        {
            string sql = string.Format(SQL_DELETE_T_BUDGET,obj.Db_OID);
            DBHelper.Excute(sql);
        }

        public void Update(T_BudgetObject obj)
        {
            string sql = string.Format(SQL_UPDATE_T_BUDGET, obj.Db_BeginDate, obj.Db_EndDate, obj.Db_PayCategory, obj.Db_BudgetSum, obj.Db_ReferValue,obj.Db_OID);
            DBHelper.Excute(sql);
        }

        public IList<T_BudgetObject> QueryAll()
        {
            IList<T_BudgetObject> list = new List<T_BudgetObject>();
            DataTable dtT_BudgetObject = new DataTable("T_BudgetObject");
            DBHelper.FillData(SQL_SELECT_T_BUDGET, dtT_BudgetObject);
            foreach (DataRow item in dtT_BudgetObject.Rows)
            {
                T_BudgetObject o = new T_BudgetObject();
                o.Data2Object(item);
                list.Add(o);
            }
            return list;
        }

        public T_BudgetObject Get(object id)
        {
            T_BudgetObject o = new T_BudgetObject();
            DataTable dtT_BudgetObject = new DataTable("T_BudgetObject");
            DBHelper.FillData(string.Format(SQL_SELECT_SINGLE_T_BUDGET, id), dtT_BudgetObject);
            foreach (DataRow item in dtT_BudgetObject.Rows)
            {
                o.Data2Object(item);
            }
            return o;
        }

        #endregion
    }
}
